package com.zy.util;

/**
 * @author hanhuafeng
 * @version V1.0
 * @description
 * @date 2023/5/17
 */
public class VersionCompareUtil {
    /**
     * 版本号比较
     *
     * @param v1 版本号1
     * @param v2 版本号2
     * @return true代表左边大于等于右边，false代表右边大
     * VersionCompareUtil.compareVersion("1.0.3", "1.20")
     */
    public static boolean compareVersion(String v1, String v2) {
        boolean flag = true;
        String[] appVersion1 = v1.split("\\.");
        String[] appVersion2 = v2.split("\\.");
        //根据位数最短的判断
        int lim = Math.min(appVersion1.length, appVersion2.length);
        //根据位数循环判断各个版本
        for (int i = 0; i < lim; i++) {
            if (Integer.parseInt(appVersion1[i]) > Integer.parseInt(appVersion2[i])) {
                break;
            }
            if (Integer.parseInt(appVersion1[i]) < Integer.parseInt(appVersion2[i])) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    public static void main(String[] args) {
        System.out.println(VersionCompareUtil.compareVersion("1.0.3", "1.20"));
    }
}
